package com.xy.manage.action;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.xy.common.page.Pagination;
import com.xy.common.util.DictUtils;
import com.xy.common.security.BadCredentialsException;
import com.xy.common.security.UsernameNotFoundException;
import com.xy.common.web.CookieUtils;
import com.xy.common.web.RequestUtils;
import com.xy.core.web.WebErrors;

import com.xy.manage.model.Tmember;
import com.xy.manage.service.TmemberService;



@Controller
public class TmemberAction{
	private static final Logger log = LoggerFactory.getLogger(TmemberAction.class);
	public TmemberAction() {
	}
 
 
 	/**
	 * 会员信息新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmemberAdd.do")
	public String add(HttpServletRequest request, ModelMap model) {
		model.addAttribute("tmember", new Tmember());
        setDict(model);
		return "tmemberAdd";
	}

	/**
	 * 会员信息新增保存
	 * 
	 * @param tmember
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/o_tmemberSave.do")
	public String save(Tmember tmember, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(tmember, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		tmember = tmemberService.save(tmember);
		String actiontype=request.getParameter("actiontype");
		if(actiontype!=null){
			return "saveOK";
		}
		return "redirect:v_tmemberList.do";
	}

	/**
	 * 会员信息删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("o_tmemberDelete.do")
	public String delete(long[] ids) {
		if (ids != null) {
			for (long id : ids) {
				tmemberService.remove(id);
			}
		}
		return "redirect:v_tmemberList.do";
	}

	/**
	 * 会员信息编辑页面
	 * 
	 * @param memberid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmemberEdit.do")
	public String edit(long memberid, HttpServletRequest request, ModelMap model) {
		Tmember tmember = tmemberService.get(memberid);
		model.addAttribute("tmember", tmember);
		model.addAttribute("actionEdit", request.getParameter("request"));
        setDict(model);
		return "tmemberEdit";
	}

	/**
	 * 会员信息详情
	 * 
	 * @param memberid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmemberDetail.do")
	public String detail(long memberid, HttpServletRequest request, ModelMap model) {
		Tmember tmember = tmemberService.get(memberid);
		model.addAttribute("tmember", tmember);
        setDict(model);
		return "tmemberView";
	}

	/**
	 * 会员信息查询（分页）
	 * 
	 * @param tmember
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmemberList.do")
	public String listPage(Tmember tmember, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		if (pageNo == null)
			pageNo = 1;
		Pagination pagination = tmemberService.getTmemberByPage(toMap(tmember),
				pageNo, CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
        setDict(model);
		return "tmemberList";
	}

	/**
	 * 会员信息查询
	 * 
	 * @param tmember
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmemberList1.do")
	public String list(Tmember tmember, HttpServletRequest request, ModelMap model) {
		List<Tmember> list = tmemberService.getTmember(toMap(tmember));
		model.addAttribute("list", list);
        setDict(model);
		return "tmemberList";
	}
	
	@RequestMapping("o_tmemberChangeScore.do")
	public String changeScore(String username, HttpServletRequest request, ModelMap model,String returnPath,int score) {
		Map<String,Object> map =new HashMap<String, Object>();
		map.put("loginname", username);
		List<Tmember> tmembers = tmemberService.getTmember(map);
		if(null!=tmembers&&tmembers.size()>0)
			tmemberService.changeScore(tmembers.get(0), score);
		return "redirect: "+returnPath;
	}
	
	


 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param tmember
	 * @return
	 */	private Map<String,Object> toMap(Tmember tmember){	
		 Map<String,Object> map=new HashMap<String, Object>();
		 if(tmember.getMemberid()!=null&&!tmember.getMemberid().equals("")){
			map.put("memberid", tmember.getMemberid());
		}
		if(tmember.getLoginname()!=null&&!tmember.getLoginname().equals("")){
			map.put("loginname", tmember.getLoginname());
		}
		if(tmember.getPhone()!=null&&!tmember.getPhone().equals("")){
			map.put("phone", tmember.getPhone());
		}
		if(tmember.getPasswd()!=null&&!tmember.getPasswd().equals("")){
			map.put("passwd", tmember.getPasswd());
		}
		if(tmember.getTruename()!=null&&!tmember.getTruename().equals("")){
			map.put("truename", tmember.getTruename());
		}
		if(tmember.getSex()!=null&&!tmember.getSex().equals("")){
			map.put("sex", tmember.getSex());
		}
		if(tmember.getIdcard()!=null&&!tmember.getIdcard().equals("")){
			map.put("idcard", tmember.getIdcard());
		}
		if(tmember.getJSZ()!=null&&!tmember.getJSZ().equals("")){
			map.put("jsz", tmember.getJSZ());
		}
		if(tmember.getCarNo()!=null&&!tmember.getCarNo().equals("")){
			map.put("carno", tmember.getCarNo());
		}
		if(tmember.getFDJH()!=null&&!tmember.getFDJH().equals("")){
			map.put("fdjh", tmember.getFDJH());
		}
		if(tmember.getHPZL()!=null&&!tmember.getHPZL().equals("")){
			map.put("hpzl", tmember.getHPZL());
		}
		if(tmember.getEmail()!=null&&!tmember.getEmail().equals("")){
			map.put("email", tmember.getEmail());
		}
		if(tmember.getScore()!=null&&!tmember.getScore().equals("")){
			map.put("score", tmember.getScore());
		}
		if(tmember.getRegtime()!=null&&!tmember.getRegtime().equals("")){
			map.put("regtime", tmember.getRegtime());
		}
		if(tmember.getModtime()!=null&&!tmember.getModtime().equals("")){
			map.put("modtime", tmember.getModtime());
		}
		if(tmember.getStatus()!=null&&!tmember.getStatus().equals("")){
			map.put("status", tmember.getStatus());
		}
		if(tmember.getDABH()!=null&&!tmember.getDABH().equals("")){
			map.put("dabh", tmember.getDABH());
		}
		if(tmember.getJSZ2()!=null&&!tmember.getJSZ2().equals("")){
			map.put("jsz2", tmember.getJSZ2());
		}
		if(tmember.getCarNo2()!=null&&!tmember.getCarNo2().equals("")){
			map.put("carno2", tmember.getCarNo2());
		}
		if(tmember.getFDJH2()!=null&&!tmember.getFDJH2().equals("")){
			map.put("fdjh2", tmember.getFDJH2());
		}
		if(tmember.getHPZL2()!=null&&!tmember.getHPZL2().equals("")){
			map.put("hpzl2", tmember.getHPZL2());
		}
		if(tmember.getDABH2()!=null&&!tmember.getDABH2().equals("")){
			map.put("dabh2", tmember.getDABH2());
		}
		if(tmember.getJSZ3()!=null&&!tmember.getJSZ3().equals("")){
			map.put("jsz3", tmember.getJSZ3());
		}
		if(tmember.getCarNo3()!=null&&!tmember.getCarNo3().equals("")){
			map.put("carno3", tmember.getCarNo3());
		}
		if(tmember.getFDJH3()!=null&&!tmember.getFDJH3().equals("")){
			map.put("fdjh3", tmember.getFDJH3());
		}
		if(tmember.getHPZL3()!=null&&!tmember.getHPZL3().equals("")){
			map.put("hpzl3", tmember.getHPZL3());
		}
		if(tmember.getDABH3()!=null&&!tmember.getDABH3().equals("")){
			map.put("dabh3", tmember.getDABH3());
		}
		if(tmember.getUlevel()!=null&&!tmember.getUlevel().equals("")){
			map.put("ulevel", tmember.getUlevel());
		}
		return map;}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
      model.addAttribute("sexMap", DictUtils.getDictByType(DictUtils.TMEMBER_SEX));
      model.addAttribute("HPZLMap", DictUtils.getDictByType(DictUtils.TMEMBER_HPZL));
      model.addAttribute("HPZL2Map", DictUtils.getDictByType(DictUtils.TMEMBER_HPZL2));
      model.addAttribute("HPZL3Map", DictUtils.getDictByType(DictUtils.TMEMBER_HPZL3));	
      model.addAttribute("ulevelMap", DictUtils.getDictByType(DictUtils.TMEMBER_ULEVEL));	
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Tmember tmember, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}


	/**
	 * 检查登陆名是否已经存在
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/check_loginname.do")
	public void check_loginname(ModelMap model, HttpServletRequest request,HttpServletResponse response)
			throws Exception {
      try{
		String loginname=request.getParameter("loginname");
		if(loginname==null||loginname.equals("")){
			response.getWriter().write("false");
			return;
		}
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("loginname", loginname);
		List l = tmemberService.getTmember(param);
		String text=l.size()==0?"true":"false";         
  		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		response.getWriter().write(text);
      } catch (Exception e) {
			log.error(e.toString());
         throw e;
		}
      
	}   
      
	@Resource
	private TmemberService tmemberService;   
}